import { defineStore } from "pinia";
import _ from "lodash";

// 第一个参数是应用程序中 store 的唯一 id
export const batchCreateAdStore = defineStore("batchCreateAdStore", {
  // 其它配置项
  state: () => {
    const obj: any = {
      _id: 0,
      AdxId: 0,
      AdxSubId: 0,
      Name: "",
      LabelIds: [],
      Strategy: {
        common: true,
        targets_rule: "NORMAL",
        titles_rule: "NORMAL",
      },
      Plan: {
        converBidMap: {
          // 22454: { type: "NORMAL_BID", value: 66 },
          // 22455: { type: "NORMAL_BID", value: 4 },
        },
      },
      Campaign: {
        // audienceMap: {
        //   22454: { include: [1], exclude: [2] },
        //   22455: { include: [], exclude: [] },
        // },
      },
      // 素材
      CreativeCategoryPackageIds: [],
      CreativeLabelPackageIds: [],
      CreativeCategoryPackageItems: [],
      CreativeLabelPackageItems: [],
      CreativeConfig: {},
      Creatives: [],

      AccountIds: [],
      AccountItems: [
        { _id: 22454, Name: "广告账号A" },
        { _id: 22455, Name: "广告账号B" },
        // { _id: 22456, Name: "广告账号C" },
      ], //已选账户

      TargetIds: [],
      TargetIdsMap: {},
      TargetItems: [], //已选定向包

      TitlePackageIds: [], //已选标题包id
      TitlePackageItems: [], //已选标题包
      CardTitlePackageItems: [], //已选商品卡标题包
      TitlePackages: [],
      CopyWriteIds: [],
      // Titles: [], //标题、文案

      landingPageItems: [], // 落地页组
      // 落地页
      Landpage: {},
      Monitor: {},

      key: 1,
    };
    return obj;
  },
  getters: {
    // getName: (state) => {
    //   return state.Name;
    // },
  },
  actions: {
    getData(type: string) {
      // 先深复制一份，避免数据干扰
      let data = _.cloneDeep(this.$state);

      data.AccountIds = (data.AccountItems || []).map((item) => item._id);
      data.TargetIds = (data.TargetItems || []).map((item) => item._id);
      data.TitlePackageIds = (data.TitlePackageItems || []).map((item) => item._id);
      data.CardTitlePackageIds = (data.CardTitlePackageItems || []).map((item) => item._id);

      let sendData: any = data;

      return sendData;
    },
    setData(data) {
      this.$state = data;
    },
    reset() {
      this.$reset();
    },
  },
});
